import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: "/login",
    component: () => import("../components/common/Login")
  },
  {
    path: "/home",
    component: () => import("../views/home/Home"),
    redirect: "/welcome",
    children: [
      {
        path: "/welcome",
        component: () => import("../views/welcome/Welcome")
      },
      {
        path: "/users",
        component: () => import("../views/user/Users")
      },
      {
        path: "/rights",
        component: () => import("../views/power/Rights")
      },
      {
        path: "/roles",
        component: () => import("../views/power/Roles")
      },
      {
        path: "/categories",
        component: () => import("../views/goods/Cate")
      },
      {
        path: "/params",
        component: () => import("../views/goods/Params")
      },
      {
        path: "/goods",
        component: () => import("../views/goods/List")
      }, 
      {
        path: "/goods/add",
        component: () => import("../views/goods/Add")
      },
      {
        path: "/orders",
        component: () => import("../views/orders/Order")
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 设置路由导航守卫，控制权限
router.beforeEach((to, from, next) => {
  // 如果用户 访问 登录页面，直接放行
  if (to.path === "/login") return next()
  // 取出token
  const token = window.sessionStorage.getItem('token')
  if (!token) {
    Vue.prototype.$message.warning("登录信息过期 请重新登录！")
    window.setTimeout(() => {
      return next('/login')
    }, 1000)
  }
  next()
})
export default router
